package com.acme.bankapp.domain.bank;

import com.acme.bankapp.exceptions.NotEnoughFundsException;
import com.acme.bankapp.exceptions.OverDraftLimitExceededException;

/**
 * This is checking variant of particular implementation of abstract account
 * 
 * @author Anton Kotov
 * 
 */
public class CheckingAccount extends AbstractAccount {

	private double overdraft;

	public CheckingAccount(final int id, final double balance,
			final double overdraft) {
		super(id, balance);
		
		if (balance + overdraft < 0.) {
			throw new IllegalArgumentException();
		}
				
		this.overdraft = overdraft;
	}

	public void withdraw(final double amount)
			throws OverDraftLimitExceededException {
		if (amount <= 0.) {
			throw new IllegalArgumentException();
		}
		try {
			super.withdraw(amount);
		} catch (NotEnoughFundsException e) {
			throw new OverDraftLimitExceededException("Failed to withdraw: account is " + this, e);
		}
		assert (balance + overdraft >= 0.) : "balance + overdraft is negative";
	}

	@Override
	public double maximumAmountToWithdraw() {
		return getBalance() + overdraft;
	}
}
